package com.bangdream.michelia.view.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bangdream.michelia.R;
import com.bangdream.michelia.application.AppQuestData;
import com.bangdream.michelia.entity.QuestAnswerBean;
import com.bangdream.michelia.entity.exam.AnswerBean;
import com.bangdream.michelia.entity.exam.ExamQuestBean;
import com.bangdream.michelia.tool.FullyGridLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @date on 2018/8/10 23:42
 * @email 909732184@qq.com
 * 问卷的列表适配器
 */
public class QuestListAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<ExamQuestBean> data = new ArrayList<>();
    private OnItemClickListener onItemClickListener;
    private ExamQuestBean.QuestionInfoListBean questListBeen = new ExamQuestBean.QuestionInfoListBean();
    private List<ExamQuestBean.AnswerInfoListBean> answerInfoListBeanList = new ArrayList<>();
    private ListItemClickHelp callBack;
    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public void notifyDataChange(List<ExamQuestBean> data){
        this.data  = data;
        notifyDataSetChanged();
    }

    /*public List<ExamQuestBean> getData() {
        return data;
    }

    public void setData(List<ExamQuestBean> data) {
        this.data = data;
    }*/

    public interface OnItemClickListener{
        void onQrderItemClick(int position);
    }
    public QuestListAdapter(Context context,ListItemClickHelp callBack,List<ExamQuestBean> data) {
        this.context = context;
        this.callBack = callBack;
        this.data = data;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == 0) {//选择题
            View view = layoutInflater.inflate(R.layout.quest_adapter_item1, parent, false);
            return new ViewHolder1(view);
        }else if(viewType == 1){ //填空题
            View view = layoutInflater.inflate(R.layout.quest_adapter_item2, parent, false);
            return new ViewHolder2(view);
        }else if(viewType == 2){//判断题
            View view = layoutInflater.inflate(R.layout.quest_adapter_item3, parent, false);
            return new ViewHolder3(view);
        } else if(viewType == 3){//选择题
            View view = layoutInflater.inflate(R.layout.quest_adapter_item4, parent, false);
            return new ViewHolder4(view);
        }else if (viewType == 4){
            View view = layoutInflater.inflate(R.layout.quest_adapter_item5, parent, false);
            return new ViewHolder5(view);
        }else{
            View view = layoutInflater.inflate(R.layout.quest_adapter_item6, parent, false);
            return new ViewHolder6(view);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if(position<data.size()){
            Log.i("question--",""+JSONObject.toJSON(data.get(position)).toString());
        }
        ViewHolder1 viewHolder1 = null;
        ViewHolder2 viewHolder2 = null;
        ViewHolder3 viewHolder3 = null;
        ViewHolder4 viewHolder4 = null;
        ViewHolder5 viewHolder5 = null;
        ViewHolder6 viewHolder6 = null;

        final List<QuestAnswerBean> questList = AppQuestData.getInstance().getQuestAnswerList();
        if(holder instanceof ViewHolder1){          //多选题
            viewHolder1 = (ViewHolder1) holder;
            questListBeen = data.get(position).getQuestionInfoList();
            viewHolder1.tvTitle.setText(questListBeen.getDescription());
//            viewHolder1.tvTitle.setText(data.get(position).getTitle());
            Log.i("question==",""+JSONObject.toJSON(data.get(position)).toString());
            viewHolder1.tvNumber.setText(position+1+"");
//            JSONObject json = (JSONObject) JSONObject.toJSON(questListBeen);
            JSONArray array = (JSONArray) JSONArray.toJSON(data.get(position).getAnswerInfoList());
            answerInfoListBeanList = JSON.parseArray(String.valueOf(array),ExamQuestBean.AnswerInfoListBean.class);
//            JSONObject ansjson = (JSONObject) JSONObject.toJSON(answerInfoListBeanList.get(0));
            /*Log.i("question",""+json.toString());
            for(int i=0;i<answerInfoListBeanList.size();i++){
                Log.i("answer",""+  JSONObject.toJSON(answerInfoListBeanList.get(i)).toString());
            }*/String infoId = data.get(position).getInfoId();
            String id = data.get(position).getId();
            viewHolder1.answerGridItemAdapter.setNmPostion(answerInfoListBeanList.size());
            viewHolder1.answerGridItemAdapter.setData(answerInfoListBeanList,infoId,id);
            viewHolder1.answerGridItemAdapter.notifyDataSetChanged();
        }else if(holder instanceof ViewHolder2){//填空题
            viewHolder2 = (ViewHolder2) holder;
            viewHolder2.tvNumber.setText(position+1+"");
            questListBeen = data.get(position).getQuestionInfoList();
            Log.i("question==",""+JSONObject.toJSON(data.get(position)).toString());
            viewHolder2.tvTitle.setText(questListBeen.getDescription());
//            viewHolder2.tvTitle.setText(data.get(position).getTitle());
            viewHolder2.etWrite.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void afterTextChanged(Editable s) {
                    if(s.length()>1){
                        QuestAnswerBean bean = new QuestAnswerBean();
                        bean.setId(questListBeen.getId());
                        bean.setQuestionId(questListBeen.getQuestionId());
                        bean.setAnswer(s.toString());
                        bean.setPosition(position);
                        if(questList.size()>0){
                            if(questList.get(position).getQuestionId().equals(questListBeen.getQuestionId())){
                                questList.set(position,bean);
                            }else{
                                questList.add(position,bean);
                            }
                        }else{
                            questList.add(position,bean);
                        }
                        questList.forEach(item->Log.e("position",item.getQuestionId()));

                    }
                }
            });

        }else if(holder instanceof ViewHolder3){    //判断题
           viewHolder3 = (ViewHolder3) holder;
            viewHolder3.tvNumber.setText(position+1+"");
            questListBeen = data.get(position).getQuestionInfoList();
            Log.i("question==",""+JSONObject.toJSON(data.get(position)).toString());
            JSONArray array = (JSONArray) JSONArray.toJSON(data.get(position).getAnswerInfoList());
            answerInfoListBeanList = JSON.parseArray(String.valueOf(array),ExamQuestBean.AnswerInfoListBean.class);
//            viewHolder3.ivOptionTrue.setTag(viewHolder3);
            viewHolder3.tvTitle.setText(questListBeen.getDescription());
//            viewHolder3.tvTitle.setText(data.get(position).getTitle());
            viewHolder3.selectAdapter.setNmPostion(answerInfoListBeanList.size());
//            viewHolder4.selectAdapter.setData(answerInfoListBeanList);
            String infoId = data.get(position).getInfoId();
            String id = data.get(position).getId();
            viewHolder3.selectAdapter.notifyDataChange(answerInfoListBeanList,infoId,id,position);
//            viewHolder3.tvTitle.setText(questListBeen.getDescription());
        }else if(holder instanceof ViewHolder4){ //单选题
            viewHolder4 = (ViewHolder4) holder;
            viewHolder4.tvNumber.setText(position+1+"");
            questListBeen = data.get(position).getQuestionInfoList();
            Log.i("question==",""+JSONObject.toJSON(data.get(position)).toString());
            JSONArray array = (JSONArray) JSONArray.toJSON(data.get(position).getAnswerInfoList());
            answerInfoListBeanList = JSON.parseArray(String.valueOf(array),ExamQuestBean.AnswerInfoListBean.class);
            viewHolder4.selectAdapter.setNmPostion(answerInfoListBeanList.size());
//            viewHolder4.selectAdapter.setData(answerInfoListBeanList);
            String infoId = data.get(position).getInfoId();
            String id = data.get(position).getId();
            viewHolder4.selectAdapter.notifyDataChange(answerInfoListBeanList,infoId,id,position);
//            viewHolder4.selectAdapter.notifyDataChange(answerInfoListBeanList,position);
            viewHolder4.tvTitle.setText(questListBeen.getDescription());
//            viewHolder4.tvTitle.setText(data.get(position).getTitle());
        }else if(holder instanceof ViewHolder5){        //简答题
            viewHolder5 = (ViewHolder5) holder;
            viewHolder5.tvNumber.setText(position+1+"");
            questListBeen = data.get(position).getQuestionInfoList();
            Log.i("question==",""+JSONObject.toJSON(data.get(position)).toString());
            JSONArray array = (JSONArray) JSONArray.toJSON(data.get(position).getAnswerInfoList());
            answerInfoListBeanList = JSON.parseArray(String.valueOf(array),ExamQuestBean.AnswerInfoListBean.class);
            viewHolder5.tvTitle.setText(questListBeen.getDescription());
//            viewHolder5.tvTitle.setText(data.get(position).getTitle());
            viewHolder5.etWrite.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void afterTextChanged(Editable s) {
                    if(s.length()>1){
                        QuestAnswerBean bean = new QuestAnswerBean();
                        bean.setId(questListBeen.getId());
                        bean.setQuestionId(questListBeen.getQuestionId());
                        bean.setAnswer(s.toString());
                        bean.setPosition(position);
                        questList.forEach(item->Log.e("position",item.getQuestionId()));

                        boolean existFlag  = checkIdExist(questList,questListBeen.getQuestionId());

                        if(existFlag){
                            if(questList.size()>=position+1){
                                questList.set(position,bean);
                            }else{
                                questList.set(questList.size(),bean);
                            }
                        }else{
                            if(questList.size()>=position+1){
                                questList.add(position,bean);
                            }else{
                                questList.add(questList.size(),bean);
                            }
                        }

                    }
                }
            });
        }else{
            viewHolder6 = (ViewHolder6) holder;
            final int clickId = viewHolder6.btnGo.getId();
//            final List<ExamQuestBean.AnswerInfoListBean> data =   viewHolder4.selectAdapter.getCurData();
            Log.i("data---",""+data.toString());
            viewHolder6.btnGo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("选择的数字为==","-----");
                    callBack.onClick(position,clickId);
                }
            });
            if(data.size()<1){
                viewHolder6.rl_submit.setVisibility(View.GONE);
//            viewHolder6.btnGo.setVisibility(View.GONE);
            }else{
                viewHolder6.rl_submit.setVisibility(View.VISIBLE);
            }
        }
        if(holder instanceof ViewHolder1){

        }

    }
    private boolean checkIdExist(List<QuestAnswerBean> questList,String id){
        for(int i=0;i<questList.size();i++){
            if(questList.get(i).getQuestionId().equals(id)){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    @Override
    public int getItemCount() {
        //最后加一条item为提交按钮
        return data==null?1:data.size()+1;
//        return data.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        //多一条item放提交按钮
        String type="" ;
        if(position<data.size() ){
            type = data.get(position).getQuestionType();
        }
        if(type.equals("multiSelection")){ //选择题
            return 0;
        }else if(type.equals("write")){ //填空题
            return 1;
        }else if(type.equals("judgement")){ //判断题
            return 2;
        }else if(type.equals("selection")) { //选择题题
            return 3;
        } else if(type.equals("short")){ //简答题
            return 4;
        }else{
            return 5;
        }
    }

    class ViewHolder1 extends RecyclerView.ViewHolder{
        private FullyGridLayoutManager fullyGridLayoutManager;
        private RecyclerView recyclerView;
        private TextView tvTitle;
        private TextView tvNumber;
        //答案网格布局
        public QuestAnswerGridItemAdapter answerGridItemAdapter;
        public ViewHolder1(View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.recyclerView);
            fullyGridLayoutManager = new FullyGridLayoutManager(context,2);
            recyclerView.setLayoutManager(fullyGridLayoutManager);
            answerGridItemAdapter = new QuestAnswerGridItemAdapter(context);
            recyclerView.setAdapter(answerGridItemAdapter);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvNumber= itemView.findViewById(R.id.tvNumber);
        }
    }

    class ViewHolder2 extends RecyclerView.ViewHolder{
        private TextView tvTitle;
        private TextView tvNumber;
        private EditText etWrite;

        public ViewHolder2(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvNumber= itemView.findViewById(R.id.tvNumber);
            etWrite = itemView.findViewById(R.id.et_write);
        }
    }
    class ViewHolder3 extends RecyclerView.ViewHolder{
        private FullyGridLayoutManager fullyGridLayoutManager;
        private RecyclerView recyclerView;
        private TextView tvTitle;
        private TextView tvNumber;
        private QuestAnswerSelectAdapter selectAdapter;
        public ViewHolder3(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvNumber= itemView.findViewById(R.id.tvNumber);
            recyclerView = itemView.findViewById(R.id.recyclerView);
            fullyGridLayoutManager = new FullyGridLayoutManager(context,2);
            recyclerView.setLayoutManager(fullyGridLayoutManager);
            selectAdapter = new QuestAnswerSelectAdapter(context);
            recyclerView.setAdapter(selectAdapter);
        }
    }
    class ViewHolder4 extends RecyclerView.ViewHolder{
        private FullyGridLayoutManager fullyGridLayoutManager;
        private RecyclerView recyclerView;
        private TextView tvTitle;
        private TextView tvNumber;
        //答案网格布局
        private QuestAnswerSelectAdapter selectAdapter;
        public ViewHolder4(View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.recyclerView);
            fullyGridLayoutManager = new FullyGridLayoutManager(context,2);
            recyclerView.setLayoutManager(fullyGridLayoutManager);
            selectAdapter = new QuestAnswerSelectAdapter(context);
            recyclerView.setAdapter(selectAdapter);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvNumber= itemView.findViewById(R.id.tvNumber);
        }
    }
    class ViewHolder5 extends RecyclerView.ViewHolder{
        private TextView tvTitle;
        private TextView tvNumber;
        private EditText etWrite;
        public ViewHolder5(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvNumber= itemView.findViewById(R.id.tvNumber);
            etWrite = itemView.findViewById(R.id.et_short);
        }
    }
    class ViewHolder6 extends RecyclerView.ViewHolder{
        private TextView btnGo;
        private RelativeLayout rl_submit;
        public ViewHolder6(View itemView) {
            super(itemView);
            btnGo = itemView.findViewById(R.id.btnGo);
            rl_submit = itemView.findViewById(R.id.rl_submit);
        }
    }
}
